package com.hm.ibsm.util;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;

import com.hm.common.def.DataStatusDef;

import lombok.Data;

/**
 * @author shishun.wang
 * @date 下午10:37:38 2017年2月1日
 * @version 1.0
 * @describe
 */
@Data
@MappedSuperclass
public class BaseDomain implements Serializable {

	private static final long serialVersionUID = 8581298879469978894L;

	@Id
	// @GeneratedValue
	@GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid")
	private String id;

	@Column(name = "create_at", nullable = false, insertable = true, updatable = false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date createAt;
	// private Long createAt;

	@Column(name = "last_update_at", nullable = false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date lastUpdateAt;
	// private Long lastUpdateAt;

	@Column(name = "status", nullable = false)
	@Enumerated(EnumType.STRING)
	private DataStatusDef status = DataStatusDef.ENABLE;

	@PrePersist
	protected void settingCreateAt() {
		if (createAt == null) {
			createAt = new Date();
			lastUpdateAt = createAt;
		}
	}

	@PreUpdate
	protected void settingLastUpdateAt() {
		if (id != null) {
			lastUpdateAt = new Date();
		}
	}

	public static String[] ignoreColumn() {
		return new String[] { "id", "createAt", "lastUpdateAt", "status" };
	}
}
